﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Jumpton.SFA.Server.BusinessRule.CPR;
using Jumpton.SFA.Server.Entity;
using System.Data;
using System.Collections;
using Jumpton.SFA.Server.BusinessRule;
using Jumpton.Persistent;

public partial class CPR_Form_Config :PageBase
{
    private const string MeasuresGroup_List = "CPR_Form_Config.aspx";

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            
            ViewState["KPRCPR_InputTYpe"] = new DictionaryItemsManager().GetItemList(DictionaryEnum.KPRCPR_InputTYpe, MyStateManager.DomainID);
            GetGridViewBind();
        }
    }

    /// <summary>
    /// 数据绑定
    /// </summary>
    public void GetGridViewBind()
    {
        GridView1.DataSource = new CPR_Form_Manager().GetCPRConfig(MyStateManager.DomainID.ToString());
        GridView1.DataBind();
        GridView1.Visible = GridView1.Rows.Count > 0;
    }

    /// <summary>
    /// 保存数据事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            CPR_Form_Manager manager = new CPR_Form_Manager();
            CPR_FORM_CONFIG entity = new CPR_FORM_CONFIG();
            if (DetailsView1.CurrentMode == DetailsViewMode.Insert)
            {
                entity.CREATE_TIME = Jumpton.SFA.Server.BusinessRule.BusinessLogic.GetDateTimeDecimal(DateTime.Now);
            }
            else
            {
                entity = manager.Load(int.Parse(GridView1.SelectedDataKey.Value.ToString()));
            }

            TextBox TextBox1 = DetailsView1.FindControl("TextBox1") as TextBox;
            TextBox TextBox2 = DetailsView1.FindControl("TextBox2") as TextBox;
            TextBox TextBox3 = DetailsView1.FindControl("TextBox3") as TextBox;
            TextBox TextBox4 = DetailsView1.FindControl("TextBox4") as TextBox;
            TextBox TextBox5 = DetailsView1.FindControl("TextBox5") as TextBox;
            TextBox txtMinValue = DetailsView1.FindControl("txtMinValue") as TextBox;      //数值和小数输入的最小值
            TextBox txtMaxValue = DetailsView1.FindControl("txtMaxValue") as TextBox;      //数值和小数输入的最大值
            TextBox txtMaxLength = DetailsView1.FindControl("txtMaxLenght") as TextBox;    //文本输入的最大长度
            DropDownList DropDownList1 = DetailsView1.FindControl("DropDownList1") as DropDownList;
            CheckBox CheckBox1 = DetailsView1.FindControl("CheckBox1") as CheckBox;
            CheckBox CheckBox2 = DetailsView1.FindControl("CheckBox2") as CheckBox;
            CheckBox CheckBox3 = DetailsView1.FindControl("CheckBox3") as CheckBox;
            RadioButtonList rdoList = DetailsView1.FindControl("rdoList") as RadioButtonList;  //本品竞品
            RadioButtonList phototypeList = DetailsView1.FindControl("PhotoTypeRadioButtonList") as RadioButtonList;    //图片类型


            entity.CODE = TextBox1.Text;
            entity.MEASUREMENTNAME = TextBox2.Text;
            entity.INPUTTYPE = int.Parse(DropDownList1.SelectedValue);
            entity.ENUM_VALUES = "|" + TextBox5.Text.Replace("\n", "|");//下拉框值;
            entity.PICKTYPE = decimal.Parse(rdoList.SelectedValue.ToString());
            entity.PHOTO_TYPE = phototypeList.SelectedValue.ToString();
            if ("" != txtMinValue.Text.Trim())
                entity.INPUT_MINVALUE = int.Parse(txtMinValue.Text.ToString());
            if ("" != txtMaxValue.Text.Trim())
                entity.INPUT_MAXVALUE = int.Parse(txtMaxValue.Text.ToString());
            if ("" != txtMaxLength.Text.Trim())
                entity.INPUT_MAXLENGTH = int.Parse(txtMaxLength.Text.ToString());

            entity.ISREVIEW = CheckBox1.Checked;
            entity.ACTIVE = CheckBox2.Checked;
            entity.ISREQUIRED = CheckBox3.Checked;
            entity.DOMAIN_ID = MyStateManager.DomainID;
            entity.LAST_UPDATER = MyStateManager.UserID.ToString();
            entity.LAST_UPDATE_TIME = Jumpton.SFA.Server.BusinessRule.BusinessLogic.GetDateTimeDecimal(DateTime.Now);

            if (entity.State == DataRowState.Added)
            {
                if (manager.Load(entity.DOMAIN_ID, entity.CODE) != null)
                {
                    ShowMessage("代码已存在."); return;
                }
            }

            if (manager.Save(entity))
            {
                //if (!string.IsNullOrEmpty(entity.TableName))
                //    manager.CreateTable(entity.TableName);
                ShowMessage("保存成功", Request.Url.ToString());
            }
        }
        catch (Exception ex)
        {
            ShowMessage(ex.Message);
            Jumpton.SFA.Server.BusinessRule.System.EventManager.CreateNewEvent(
                Jumpton.SFA.Server.BusinessRule.System.LogType.GENERAL_EXCEPTION, this.MyStateManager.Name,
                this.MyStateManager.UserID, "CPR维护操作 - 异常:" + ex.Message);
        }
    }

    protected string getInputType(string id)
    {
        Input_Type it = new Input_Type();
        var i = from m in it.TypeNewList
                where m.ID == int.Parse(id)
                select m;

        return i.ToArray()[0].Name;
    }

    /// <summary>
    /// 编辑事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        DetailsView1.ChangeMode(DetailsViewMode.Edit);
        DetailsView1.DataBind();
    }
    /// <summary>
    /// 绑定
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void DetailsView1_DataBound(object sender, EventArgs e)
    {
        DropDownList ddl = DetailsView1.FindControl("DropDownList1") as DropDownList;

        if (ViewState["KPRCPR_InputTYpe"] != null)
            ddl.DataSource = ViewState["KPRCPR_InputTYpe"];
        else
            ddl.DataSource = new DictionaryItemsManager().GetItemList(DictionaryEnum.KPRCPR_InputTYpe, MyStateManager.DomainID);

        ddl.DataTextField = "NAME";
        ddl.DataValueField = "CODE";
        ddl.DataBind();
        //Input_Type it = new Input_Type();
        //ddl.DataSource = it.TypeNewList;
        //ddl.DataValueField = "ID";
        //ddl.DataTextField = "Name";
        //ddl.DataBind();
        CPR_FORM_CONFIG drv = null;
        RadioButtonList phototypeList = null;
        if (DetailsView1.CurrentMode == DetailsViewMode.Edit)
        {
            drv = DetailsView1.DataItem as CPR_FORM_CONFIG;
            TextBox TextBox1 = DetailsView1.FindControl("TextBox1") as TextBox;
            TextBox TextBox2 = DetailsView1.FindControl("TextBox2") as TextBox;
            //TextBox TextBox3 = DetailsView1.FindControl("TextBox3") as TextBox;
            TextBox TextBox4 = DetailsView1.FindControl("TextBox4") as TextBox;
            TextBox TextBox5 = DetailsView1.FindControl("TextBox5") as TextBox;
            TextBox txtMinValue = DetailsView1.FindControl("txtMinValue") as TextBox;     //数值和小数输入的最小值
            TextBox txtMaxValue = DetailsView1.FindControl("txtMaxValue") as TextBox;     //数值和小数输入的最大值
            TextBox txtMaxLength = DetailsView1.FindControl("txtMaxLenght") as TextBox;   //文本输入的最大长度
            DropDownList DropDownList1 = DetailsView1.FindControl("DropDownList1") as DropDownList;
            CheckBox CheckBox1 = DetailsView1.FindControl("CheckBox1") as CheckBox;
            CheckBox CheckBox2 = DetailsView1.FindControl("CheckBox2") as CheckBox;
            CheckBox CheckBox3 = DetailsView1.FindControl("CheckBox3") as CheckBox;
            RadioButtonList rdoList = DetailsView1.FindControl("rdoList") as RadioButtonList;  //本品竞品
             phototypeList = DetailsView1.FindControl("PhotoTypeRadioButtonList") as RadioButtonList;    //图片类型


            TextBox1.Text = drv.CODE;
            TextBox2.Text = drv.MEASUREMENTNAME;
            rdoList.SelectedValue = drv.PICKTYPE.ToString();
            
            //TextBox3.Enabled = false;
            TextBox4.Text = drv.SEQUENCE.ToString();
            TextBox5.Text = GetDropdownValue(drv.ENUM_VALUES);
            DropDownList1.SelectedValue = drv.INPUTTYPE.ToString();
            DropDownList1_SelectedIndexChanged(DropDownList1,e);
            if (null != drv.INPUT_MINVALUE)
                txtMinValue.Text = drv.INPUT_MINVALUE.ToString();
            if (null != drv.INPUT_MAXVALUE)
                txtMaxValue.Text = drv.INPUT_MAXVALUE.ToString();
            if (null != drv.INPUT_MAXLENGTH)
                txtMaxLength.Text = drv.INPUT_MAXLENGTH.ToString();
            CheckBox1.Checked = drv.ISREVIEW;
            CheckBox2.Checked = drv.ACTIVE;
            CheckBox3.Checked = drv.ISREQUIRED;

        }
        else
        {
            //TextBox TextBox3 = DetailsView1.FindControl("TextBox3") as TextBox;
            //TextBox3.Enabled = true;
        }
        ChangeSelect(ddl);
        if (drv!=null)
        phototypeList.SelectedValue = drv.PHOTO_TYPE == null ? null : drv.PHOTO_TYPE.Trim();
    }
    /// <summary>
    /// 新增
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        DetailsView1.ChangeMode(DetailsViewMode.Insert);
        DetailsView1.DataBind();
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        GetGridViewBind();
    }
    //protected void ddlValid_SelectedIndexChanged(object sender, EventArgs e)
    //{
    //    GetGridViewBind();
    //}
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        DropDownList ddl = (DropDownList)sender;
        ViewState["ddlValue"] = ddl.SelectedValue;
        ChangeSelect(ddl);
    }
    protected void ChangeSelect(DropDownList ddl)
    {

        if (!string.IsNullOrEmpty(ViewState["ddlValue"] as string))
        {
            ddl.SelectedValue = ViewState["ddlValue"] as string;
        }
        //显示枚举值输入框
        if (ddl.SelectedValue == "1" || ddl.SelectedValue == "5" || ddl.SelectedValue == "8" || ddl.SelectedValue == "9")
        {
            DetailsView1.Fields[4].Visible = true;
        }
        else
        {
            DetailsView1.Fields[4].Visible = false;
        }
        //显示文本长度输入框
        if (ddl.SelectedValue == "4")
        {
            DetailsView1.Fields[6].Visible = true;
        }
        else
        {
            DetailsView1.Fields[6].Visible = false;
        }
        //显示最小最大值范围输入框
        if (ddl.SelectedValue == "2" || ddl.SelectedValue == "3")
        {
            DetailsView1.Fields[5].Visible = true;
        }
        else
        {
            DetailsView1.Fields[5].Visible = false;
        }
        if (ddl.SelectedValue == "6")   //图片类型
        {
            DetailsView1.Fields[9].Visible = true;
            PhotoManager pm = new PhotoManager();

            DictionaryItemsManager dm = new DictionaryItemsManager();
            RadioButtonList cl = (RadioButtonList)DetailsView1.FindControl("PhotoTypeRadioButtonList");
            DataTable ob = dm.GetItemList(DictionaryEnum.PhotoType, MyStateManager.DomainID).BillDataTable;
            cl.DataSource = ob;
            cl.DataTextField = "NAME";
            cl.DataValueField = "CODE";
            cl.DataBind();
          //  cl.SelectedValue =
        }
        else
        {
            DetailsView1.Fields[9].Visible = false;
        }

    }
    public string GetDropdownValue(string v)
    {
        if (!string.IsNullOrEmpty(v))
        {
            string split = v.Substring(0, 1);
            v = v.Substring(1);
            v = v.Replace(split, "\n");
        }

        return v;
    }



    public string getKPRCPR_InputTYpe(string v)
    {
        ObjectTable ot = null;
        if (ViewState["KPRCPR_InputTYpe"] != null)
        {
            ot = ViewState["KPRCPR_InputTYpe"] as ObjectTable;
        }
        else
        {
            ot = new DictionaryItemsManager().GetItemList(DictionaryEnum.KPRCPR_InputTYpe, MyStateManager.DomainID);
        }
        foreach (DictionaryItems d in ot)
        {
            if (d.Code == v)
            {
                return d.Name;
            }
        }
        return "";
    }
}